#12345############################

createFile
Files Sandbox MaheP Mar10

Creating a Local File

Problem
=======

Given the name of a file and it's path, how to create a file.

Solution
========

Creating a file in awk is as simple as directing the output to the name of the 
file. In the example below, we direct the output of the a print command to a 
file:

  print "Hello World!" > "MyFile.txt"

In the example above, a new file is created called MyFile.txt and the first
line of the file contains the string 

  Hello World!

If the file already exists, it is overwritten. Every execution of an awk 
program containing ">" for creating a file will result in creting a new file 
and opening a file discriptor that will be open till the completion of the
the program. What this means is that, the first call to ">" will create a file
and every subsequent call withing the same execution of the program will add 
more lines to the file.

Now, in case if we do not need to create a new file, but rather append to an 
existing file, the ">" has to be changed to ">>". 

  print "Hello World!" >> "MyFile.txt"

By default, if we do not specify the path, the default path will be the 
directory where the awk program is run from. To specify a path use:

  print "Hello World!" > "/path/to/file/MyFile.txt"

Combining the concepts above, the function to create a file is:

 function createFile(string, path, filename)
 {
     print string > path"/"filename;
 }

Author
======

Mahesh Patel



#12345############################

appendFile
Files Sandbox MaheP Mar21

Appending a Local File

Problem
=======

Given the name of a file and it's path, how to append to that file.

Solution
========

Referring to the previous example of creating a file in which the operator ">"
was used to create a file and add lines to it. The gotcha with the ">" operator
is that during the execution of the awk program, the first time the ">" operator
is invoked on a particular file, a new file is created. This means that even if
the file exists, the file is truncated to size 0 before anything is writeen to
it.

Now, in case if we do not need to create a new file, but rather append to an
existing file, the ">" has to be changed to ">>".

  print "Hello World!" >> "MyFile.txt"

The function to append to a file becomes:

 function appendFile(string, path, filename)
 {
     print string >> path"/"filename;
 }



Author
======

Mahesh Patel


#12345############################

readNthLineInfile
Files Sandbox MaheP Mar21

Reading a Particular Line in a File

Problem
=======

Given a file name and the line number in the file - "n", return the nth line.

Solution
========

There are a number ways of solving this based on the usage. to accomplise this
using a function that takes a file name and the line number in the file, the
function would have to use the readline method of reading a file. A variable is
initialized before reading of the file and is incremented every time a line is
read. A check is done at every line to see if the line number is the one that
the function passed. The function is:

 function readNthLineInfile(filename, n)
 {
     current=0;
     line="";
     while ((getline < filename) > 0)
     {
         if (++current == n)
         {
             line = $ALL;
             break;
         }
     }
     close(filename);
     return line;
 }


The above methodology assumes that the file name is passed to the function which
would make the use of this function a likely candidate in the BEGIN or END
sections of the awk code. If, however the processing of the file is done in the
"main" body of the awk code, the build-in variables NR and FNR can be used to
query the line number being processed. Consider, the snippet of code:

  awk 'BEGIN { n=64; } { if (NR == n) { ... }}' f1.txt f2.txt ... f10.txt

In the above code, the awk code is passed ten files, ranging from f1.txt to
f10.txt. the variable NR keeps count of the number of lines read so far starting
with the first file. Hence, at the end the value of NR would the the total lines
of all the files combined. The snippet above will process the the 64th line being
processed. The snippet of code illustrating FNR is:

  awk 'BEGIN { n=64; } { if (FNR == n) { ... }}' f1.txt f2.txt ... f10.txt

The variable FNR tracks the line number of the current line being processed.
This means that every time the awk program starts reading a new file, the value
of FNR is reset. In the snippet above the code will process the 64th line in
each file (assuming that each file has atleast 64 lines).


Author
======

Mahesh Patel

